<template>
  <view class="container">
    <!-- 扫描输入区域 -->
    <view class="scan-section">
      <view class="scan-header">
        <up-text :text="'扫描'+currentScanLabel" class="scan-label"></up-text>
        <up-input v-model="scanInput" :placeholder="'请扫描'+currentScanLabel" @confirm="handleScan" :focus="true"
                  class="scan-input"/>
      </view>

      <view class="status-message">
        <up-text :text="state.text" :type="statusType"></up-text>
      </view>
    </view>

    <!-- 物料信息卡片 -->
    <view class="info-section">
      <up-divider text="接料信息" lineColor="#eee" textColor="#666"></up-divider>
      <view class="info-card">
        <view class="info-row">
          <view class="info-item">
            <text class="info-label">成品名称:</text>
            <text class="info-value">{{ state.planInfo.materialName || '-' }}</text>
          </view>
          <view class="info-item">
            <text class="info-label">BOM版本:</text>
            <text class="info-value">{{ state.planInfo.positionVer || '-' }}</text>
          </view>
        </view>

        <view class="info-row">
          <view class="info-item">
            <text class="info-label">站位编号:</text>
            <text class="info-value">{{ state.positionInfo.systemRealPosition || '-' }}</text>
          </view>
          <view class="info-item">
            <text class="info-label">机器名称:</text>
            <text class="info-value">{{ state.positionInfo.machineName || '-' }}</text>
          </view>
        </view>

        <view class="info-row">
          <view class="info-item">
            <text class="info-label">物料编码:</text>
            <text class="info-value">{{ state.positionInfo.materialCode || '-' }}</text>
          </view>
          <view class="info-item">
            <text class="info-label">物料名称:</text>
            <text class="info-value">{{ state.positionInfo.materialName || '-' }}</text>
          </view>
        </view>

        <view class="info-row">
          <view class="info-item">
            <text class="info-label">物料规格:</text>
            <text class="info-value">{{ state.positionInfo.materialSpec || '-' }}</text>
          </view>
          <view class="info-item">
            <text class="info-label">物料描述:</text>
            <text class="info-value">{{ state.positionInfo.materialDesc || '-' }}</text>
          </view>
        </view>
      </view>
    </view>

    <!-- 操作按钮 -->
    <view class="button-section">
      <up-button type="default" text="重新扫描" @click="resetScan"></up-button>
    </view>
  </view>
</template>

<script setup>
import {computed, reactive, ref} from "vue";
import {getActivatePlan} from "@api/product/order/workOrderPlan.js";
import {checkMaterial, checkOldMaterial, checkPosition} from "@api/product/order/planPosition";
import {onPullDownRefresh} from '@dcloudio/uni-app'
// 扫描输入值
const scanInput = ref("");

// 当前扫描阶段
const scanStage = ref(0); // 0:站位, 1:旧物料, 2:新物料
const currentScanLabel = computed(() => {
  const labels = ['站位', '旧物料', '新物料'];
  return labels[scanStage.value];
});

const state = reactive({
  from: {
    lineId: '',
    planId: '',
    positionId: '',
    oldMaterialBarcode: '',
    materialBarcode: '',
    nextFlag: true,
  },
  planInfo: {}, // 工单计划信息
  positionInfo: {}, // 站位信息
  text: '请扫描站位条码', // 状态提示信息
});


// 根据状态计算消息类型
const statusType = computed(() => {
  if (state.text.includes('成功')) return 'success';
  if (state.text.includes('失败') || state.text.includes('错误')) return 'error';
  return 'primary';
});

// 处理扫描事件
async function handleScan() {
  try {
    switch (scanStage.value) {
      case 0: // 扫描站位
        await handleScanPosition();
        break;
      case 1: // 扫描旧物料
        await handleScanOldMaterial();
        break;
      case 2: // 扫描新物料
        await handleScanNewMaterial();
        break;
    }
  } catch (e) {
    state.text = currentScanLabel.value + "扫描失败，" + e.message;
  }
}

// 扫描站位处理
async function handleScanPosition() {
  state.from.positionId = scanInput.value;
  const res = await checkPosition(state.from);
  state.positionInfo = res.data;
  scanStage.value = 1;
  scanInput.value = "";
  state.text = "请扫描旧物料条码";
}

// 扫描旧物料处理
async function handleScanOldMaterial() {
  state.from.oldMaterialBarcode = scanInput.value;
  await checkOldMaterial({
    ...state.from,
    materialBarcode: scanInput.value
  });
  scanStage.value = 2;
  scanInput.value = "";
  state.text = "请扫描新物料条码";
}

// 扫描新物料处理
async function handleScanNewMaterial() {
  state.from.materialBarcode = scanInput.value;
  const res = await checkMaterial({
    ...state.from,
    materialBarcode: scanInput.value
  });

  state.newMaterialInfo = res.data;
  scanInput.value = "";
  state.text = "站位【" + state.positionInfo.systemRealPosition + "】接料完成";
  scanStage.value = 0;
}


// 重置扫描状态
function resetScan() {
  scanStage.value = 0;
  scanInput.value = "";
  state.positionInfo = {};
  state.text = "请扫描站位条码";
}

// 加载工单计划信息
function loadPlanInfo() {
  getActivatePlan(state.from.lineId).then(res => {
    state.planInfo = res.data;
    state.from.planId = res.data.planId;
  });
}

onShow(() => {
  state.from.lineId = uni.getStorageSync('selectLine');
  loadPlanInfo();
});

onPullDownRefresh(async () => {
  resetScan();
  uni.stopPullDownRefresh()
})
</script>

<style scoped>
.container {
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  background-color: #f8f8f8;
  min-height: 100vh;
}

/* 扫描区域样式 */
.scan-section {
  background: #fff;
  border-radius: 8px;
  padding: 12px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.05);
}

.scan-header {
  display: flex;
  align-items: center;
  margin-bottom: 8px;
}

.scan-label {
  width: 80px;
  font-size: 14px;
  color: #333;
  font-weight: bold;
}

.scan-input {
  flex: 1;
  border: 1px solid #e0e0e0;
  border-radius: 6px;
  padding: 8px 10px;
  font-size: 14px;
}

.status-message {
  padding: 6px 0;
  font-size: 13px;
}

/* 信息卡片样式 */
.info-section {
  background: #fff;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.05);
}

.info-card {
  padding: 10px 12px;
}

.info-row {
  display: flex;
  margin-bottom: 8px;
}

.info-item {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.info-label {
  font-size: 12px;
  color: #666;
  margin-bottom: 2px;
}

.info-value {
  font-size: 13px;
  color: #333;
  font-weight: 500;
}

/* 按钮区域样式 */
.button-section {
  display: flex;
  gap: 12px;
  padding: 12px;
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.05);
}

.button-section up-button {
  flex: 1;
}
</style>